using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Comteck.Dto.Orders;
using Comteck.Ktt.Services.Orders;
using Comteck.Winforms;
using Comteck.Winforms.Forms;
using Comteck.Winforms.Infrastructure;
using DevExpress.XtraBars;
using DevExpress.XtraEditors.Controls;
using Microsoft.Extensions.DependencyInjection;

namespace Comteck.Ktt.WinUI.Orders {
  /// <summary>
  /// 订单回收站
  /// </summary>
  public partial class FrmSaleOrderRecycleAdmin : FrmBase {

    #region fields

    private readonly ISaleOrderService saleOrderService;
    private List<int> selfOrgIdList;

    #endregion

    #region ctor

    /// <summary>
    /// 
    /// </summary>
    public FrmSaleOrderRecycleAdmin(ISaleOrderService saleOrderService) {
      this.saleOrderService = saleOrderService;
      InitializeComponent();
    }

    #endregion

    #region form load

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void FrmSaleOrderRecycleAdmin_Load(object sender, EventArgs e) {
      if (this.DesignMode) {
        return;
      }

      ucSaleOrder1.SetAjaxLoadHelper(ajaxLoad);

      // page
      ucSaleOrder1.ucDataNavigator1.UcDataNavigatorPaged += () => {
        btnSearch.PerformClick();
      };

      searchDateRange.BeginDate = DateTime.Now.AddDays(-2);
      BindHelper.BindWithArray<EnumSaleOrderStatus>(searchStatus, null, EnumSaleOrderStatusList.Recycle.ToArray());
      searchStatus.CheckAll();
    }

    #endregion

    #region search

    private async void btnSearch_ItemClick(object sender, ItemClickEventArgs e) {
      // search model
      var searchModel = new SaleOrderQuery() {
        Search_Date_Type = 1,
        Search_Begin_Date = searchDateRange.BeginDate,
        Search_End_Date = searchDateRange.EndDate,
        StatusList = searchStatus.GetCheckedValues<int>().ToList(),
        SelfOrgIdList = selfOrgIdList,
        Code = searchCode.EditValue.To<string>(),
        GroupNumber = searchGroupNumber.EditValue.To<string>(),
        DeliveryCode = searchDeliveryCode.EditValue.To<string>(),
        Page = 1,
      };
      await ucSaleOrder1.SearchDataAsync(searchModel);
    }

    #endregion

    #region 自提点

    private void searchSelfOrgId_Properties_ButtonClick(object sender, ButtonPressedEventArgs e) {
      switch (e.Button.Tag) {
        case 1: {
            // 选择提货点
            var form = SysGlobal.ServiceProvider.GetRequiredService<FrmSelfOrgDialog>();
            form.SelectType = EnumSelectType.Multi;
            form.ImportAction = (list) => {
              if (list.IsNullOrEmpty()) {
                return false;
              }

              selfOrgIdList = list.Select(x => x.Id).ToList();
              searchSelfOrgId.EditValue = string.Join(",", list.Select(x => x.Name));
              return true;
            };
            form.Owner = this;
            form.ShowDialog();
          }
          break;
        case 2: {
            // 清楚数据
            selfOrgIdList = null;
            searchSelfOrgId.EditValue = null;
          }
          break;
        default:
          break;
      }
    }

    #endregion

    #region 还原订单

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private async void btnUnRecycle_ItemClick(object sender, ItemClickEventArgs e) {
      await ucSaleOrder1.UnRecycleDataAsync();
    }

    #endregion

    #region 彻底删除

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private async void btnDelete_ItemClick(object sender, ItemClickEventArgs e) {
      await ucSaleOrder1.DeleteDataAsync();
    }

    #endregion

    #region export

    /// <summary>
    /// export
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void btnExport_ItemClick(object sender, ItemClickEventArgs e) {
      ucSaleOrder1.ExportDialog();
    }

    #endregion

    #region close

    /// <summary>
    /// close
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void btnClose_ItemClick(object sender, ItemClickEventArgs e) {
      this.Close();
    }

    #endregion
  }
}
